package com.lankystar.sos.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lankystar.sos.dao.ISosOrderDao;
import com.lankystar.sos.model.ResponseOrder;
import com.lankystar.sos.model.ResponseStatus;
import com.lankystar.sos.model.SosOrder;
import com.lankystar.sos.model.SosStatus;
import com.lankystar.sos.model.User;
import com.lankystar.sos.model.UserLocation;
import com.lankystar.sos.model.UserType;
import com.lankystar.util.DateUtil;

@Repository
public class SosOrderDaoImpl implements ISosOrderDao{

	@Autowired
	private SessionFactory sf;
	public Session getSession(){
		System.out.println("sosOrder开启session！！");
		return sf.getCurrentSession();
	}
	@Override
	public void addSosOrder(SosOrder record) {
		getSession().save(record);
	}
	@Override
	public Object[] selectSosOrderBysosId(Integer sosId) {
		
		String sql = "from SosOrder sos join sos.user u join u.location lo join sos.sosStatus ss where sos.sosId="+sosId+"";
		Object[] ob = (Object[]) getSession().createQuery(sql).uniqueResult();
		return ob;
	}
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> selectUserAndResp(Integer sosId) {
		/**
		 * 有响应单信息返回  ：响应单、求救单信息；
		 * 没有响应单信息返回：返回求救单信息；
		 */
		Map<String, Object> maplist = new HashMap<>();
		String sql = "select resp,rs,u,lo,ut from ResponseOrder resp left join resp.respStatus rs join resp.user u join u.location lo join u.userApprove ua join ua.userType ut join resp.sosOrder sos where sos.sosId="+sosId+"";
		List<Object[]> oblist = getSession().createQuery(sql).list();//响应单信息
		Object[] ob = {};
		if(!oblist.isEmpty()){
			ob = new Object[oblist.size()];
			int i = 0;
			for (Object[] obs : oblist) {
				Map<String, Object> map = new HashMap<>();
				ResponseOrder respOrder = (ResponseOrder) obs[0];
				ResponseStatus  respStatus= (ResponseStatus) obs[1]; 
				User user = (User) obs[2];
				UserLocation location = (UserLocation) obs[3];
				UserType userType = (UserType) obs[4];
				map.put("responseId", respOrder.getResponseId());
				map.put("lastUpdateTime", respOrder.getLastUpdateTime());
				map.put("pushCount", respOrder.getPushCount());
				map.put("respkey", respStatus.getRespkey());
				map.put("respvalue", respStatus.getRespvalue());
				map.put("userId", user.getUserId());
				map.put("userPhone", user.getUserPhone());
				map.put("userNickName", user.getUserNickName());
				map.put("userPortrait", user.getUserPortrait());
				map.put("uservalue", userType.getUservalue());
				map.put("longitude", location.getLongitude());
				map.put("latitude", location.getLatitude());
				ob[i] = map;
				i++;
			}
			maplist.put("responseOrder", ob);
		}else{
			maplist.put("responseOrder", ob);
		}
		/**
		 * 求救单  必填
		 */
		String hql = "from SosOrder sos join sos.user u join u.location lo join sos.sosStatus ss where sos.sosId="+sosId+" and ss.soskey < 100";
		Object[] obsos = (Object[]) getSession().createQuery(hql).uniqueResult();//求救单信息
		Map<String, Object> sosmap = new HashMap<>();
		SosOrder sosOrder = (SosOrder) obsos[0];
		User sosUser = (User) obsos[1];
		UserLocation soslocaltion = (UserLocation) obsos[2];
		SosStatus sosStatus = (SosStatus) obsos[3];
		sosmap.put("sosId", sosOrder.getSosId());
		sosmap.put("createTime", sosOrder.getCreateTime());
		sosmap.put("soskey", sosStatus.getSoskey());
		sosmap.put("sosvalue", sosStatus.getSosvalue());
		sosmap.put("userId", sosUser.getUserId());
		sosmap.put("userPhone", sosUser.getUserPhone());
		sosmap.put("userNickName", sosUser.getUserNickName());
		sosmap.put("userPortrait", sosUser.getUserPortrait());
		sosmap.put("longitude", soslocaltion.getLongitude());
		sosmap.put("latitude", soslocaltion.getLatitude());
		maplist.put("sosOrder", sosmap);
		return maplist;
	}
	@Override
	public void updateSosOrder(SosOrder record) {
		getSession().update(record);
	}
	@Override
	public SosOrder selectSosOrderByUserPhoneAndCreateTime(String userPhone, Date createTime) {
		String sql = "select sos from SosOrder sos join sos.user u where userPhone="+userPhone+
				" and sos.createTime = "+DateUtil.format(createTime, "yyyy-MM-dd hh:mm:ss")+"";
		SosOrder sos = (SosOrder) getSession().createQuery(sql).uniqueResult();
		return sos;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SosOrder> selectSosOrderByuserPhoneAndsosOrderStatus(String userPhone) {
		String sql = "from SosOrder sos join sos.user u join sos.sosStatus status where status.soskey<100 and u.userPhone="+userPhone+"";
		List<SosOrder> sosOrders = getSession().createQuery(sql).list();
		return sosOrders;
	}

}
